﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Threading;

namespace MyEditor;

public static class Toast
{
    public static void Show_success_Toast(string message, int durationMs = 1500)
    {
        var toast = new Border
        {
            // Background = Brushes.Black,   
            Background = new SolidColorBrush(Color.FromRgb(76, 175, 80)), 
            CornerRadius = new CornerRadius(5),
            Opacity = 0.85,
            Child = new TextBlock
            {
                Text = message,
                Foreground = Brushes.White,
                Margin = new Thickness(12),
                FontSize = 22,
                FontWeight = FontWeights.Bold // 粗体
            }
        };

        var popup = new Popup
        {
            Child = toast,
            StaysOpen = false,
            AllowsTransparency = true
        };

        // 获取屏幕大小
        double screenWidth = SystemParameters.PrimaryScreenWidth;
        double screenHeight = SystemParameters.PrimaryScreenHeight;

        // 测量 Toast 大小
        toast.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        double toastWidth = toast.DesiredSize.Width;
        double toastHeight = toast.DesiredSize.Height;

        // 计算左上角坐标，使 Toast 居中
        popup.Placement = PlacementMode.Absolute;
        popup.HorizontalOffset = (screenWidth - toastWidth) / 2;
        popup.VerticalOffset = (screenHeight - toastHeight) / 2 + 200;

        popup.IsOpen = true;

        // 定时关闭
        var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(durationMs) };
        timer.Tick += (s, e) =>
        {
            popup.IsOpen = false;
            timer.Stop();
        };
        timer.Start();
    }
}